package com.pys.service;

import java.util.List;

import org.hibernate.Session;

import com.pys.dao.impl.ProjeDAO;
import com.pys.dao.impl.TalepTipDAO;
import com.pys.pojo.PysTalepTip;
import com.pys.utils.HibernateUtil;

public class TalepTipService {

	public PysTalepTip getById(long id) {
		Session session = null;
		PysTalepTip pysTalepTip = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			TalepTipDAO talepTipDAO = new TalepTipDAO(session);
			pysTalepTip = (PysTalepTip) talepTipDAO.findById(PysTalepTip.class,
					id);
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return pysTalepTip;
	}

	public void save(PysTalepTip pysTalepTip) {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			TalepTipDAO talepTipDAO = new TalepTipDAO(session);
			talepTipDAO.save(pysTalepTip);
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			if (session != null) {
				session.getTransaction().rollback();
			}
			throw e;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	public void update(PysTalepTip pysTalepTip) {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			TalepTipDAO talepTipDAO = new TalepTipDAO(session);
			talepTipDAO.update(pysTalepTip);
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			if (session != null) {
				session.getTransaction().rollback();
			}
			throw e;
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	// public void roleEkle(String roleAd) {
	// Session session = HibernateUtil.getSessionFactory().openSession();
	// session.beginTransaction();
	// TalepTipDAO talepTipDAO = new TalepTipDAOImpl(session);
	// PysTalepTip pysTalepTip = new PysTalepTip();
	// pysTalepTip.setAd(roleAd);
	// talepTipDAO.save(pysTalepTip);
	// session.getTransaction().commit();
	// session.close();
	// }

	public List<PysTalepTip> getAllList(Boolean deleted) {
		Session session = null;
		List<PysTalepTip> list = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			TalepTipDAO talepTipDAO = new TalepTipDAO(session);
			if (deleted == null)
				list = talepTipDAO.findAll(PysTalepTip.class);
			else
				list = talepTipDAO.findByProperty(PysTalepTip.class, "deleted",
						deleted);
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return list;
	}

	public void delete(PysTalepTip pysTalepTip) {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			TalepTipDAO talepTipDAO = new TalepTipDAO(session);
			talepTipDAO.delete(pysTalepTip);
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			if (session != null) {
				session.getTransaction().rollback();
			}
			throw e;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	public List<PysTalepTip> getByName(String name) {
		Session session = null;
		List<PysTalepTip> list = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			TalepTipDAO talepTipDAO = new TalepTipDAO(session);
			list = talepTipDAO.findByProperty(PysTalepTip.class, "name", name);
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return list;
	}

	public List<PysTalepTip> getAllAktifList() {
		Session session = null;
		List<PysTalepTip> list = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			ProjeDAO projeDAO = new ProjeDAO(session);
			list = projeDAO.findByProperties(PysTalepTip.class, new String[] {
					"deleted", "durum" }, new Object[] { false, true });
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return list;
	}

}
